import json
import random
import requests
from fake_useragent import UserAgent
from urllib.parse import quote, unquote

# res = requests.get('http://www.baidu.com')
# print(res.content.decode())

# 保存图片
# url = "https://img0.baidu.com/it/u=1552106092,2626691830&fm=253&fmt=auto&app=120&f=JPEG?w=583&h=500"
#
# response = requests.get(url)
# with open("1.jpg", "wb") as f:
#     f.write(response.content)

# 保存网页
# res = requests.get('http://www.baidu.com')
# with open("baidu.html", 'w', encoding='utf-8') as f:
#     f.write(res.content.decode())
# 响应url
# print(res.url)
# 请求头
# print(res.request.headers)
# 响应头
# print(res.headers)


# 添加 User-Agent 请求头
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36'
# }
# res = requests.get('https://www.baidu.com', headers=headers)
# print(len(res.content.decode()))

# User-Agent 池
# 第一种
ualist = [
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36',
    'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1',
    'Mozilla/5.0 (Linux; Android 8.0.0; SM-G955U Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; Android 11.0; Surface Duo) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; Android) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 CrKey/1.54.248666',
]

# print(random.choice(ualist))

# 第二种
# print(UserAgent().random)

# https://www.baidu.com/s?wd=%E5%AD%A6%E4%B9%A0

# %E5%8F%82%E6%95%B0
# print(quote("参数"))
# 参数
# print(unquote("%E5%8F%82%E6%95%B0"))

# url = f"https://www.baidu.com/s?wd=${quote("参数")}"
# headers = {
#     'User-Agent': random.choice(ualist)
# }
# res = requests.get(url, headers=headers)
# print(res.content.decode())


# url = "https://www.baidu.com/s"
# headers = {
#     'User-Agent': random.choice(ualist)
# }
# name = input("请输入关键字")
# kw = {
#     'wd': name
# }
# res = requests.get(url, headers=headers, params=kw)
# print(res.content.decode())

# 保存网易云图片
# url = "https://p1.music.126.net/JfEkdZZwTIDnIq-10tJwMA==/109951170523049932.jpg?imageView&quality=89"
# headers = {
#     'User-Agent': random.choice(ualist)
# }
# res = requests.get(url, headers=headers)
# with open("网易云.jpg", "wb") as f:
#     f.write(res.content)

# 保存音乐文件
# url = "https://m10.music.126.net/20250228093708/5c63ee216b359397ddc454fced7d4471/yyaac/obj/wonDkMOGw6XDiTHCmMOi/3035928108/656b/512b/396a/a3ebbc355582fdf3b5a0772f23ad3782.m4a?vuutv=c+jtrYjJHBbLxNSjPxUYsqGMzYwOxO2efWIWZNbgjkuFV3aWKeFzU4G+ggHf7RzDU8AEmocP/regyD7XqjsL8CmzW+dBpcDzxYseGlNdBBA="
# headers = {
#     'User-Agent': random.choice(ualist)
# }
# res = requests.get(url, headers=headers)
# with open("网易云音乐.mp3", "wb") as f:
#     f.write(res.content)

# url = "https://vodkgeyttp8.vod.126.net/cloudmusic/NjM0NTA4Mg==/ff5599d855b228f17f0c316f5d755079/c48a42ac29a0b637b615493fa094dee0.mp4?wsSecret=70ef0ba2ca7e40a93c429257fd5186a7&wsTime=1740705900"
# headers = {
#     'User-Agent': random.choice(ualist)
# }
# res = requests.get(url, headers=headers)
# with open("网易云MV.mp4", "wb") as f:
#     f.write(res.content)
# 保存贴吧
# url = "https://tieba.baidu.com/f?kw=%E6%9D%8E%E6%AF%85&ie=utf-8&pn=0"
# headers = {
#     'User-Agent': random.choice(ualist)
# }
# res = requests.get(url, headers=headers)
#
# with open("李毅.html", 'wb') as f:
#     f.write(res.content)

# url = "https://tieba.baidu.com/f"
# headers = {
#     'User-Agent': random.choice(ualist)
# }
# word = input("请输入贴吧名字")
# page = int(input("请输入页码"))
# for i in range(page):
#     params = {
#         'kw': word,
#         'pn': i * 50
#     }
#     res = requests.get(url, headers=headers, params=params)
#     with open(f'{word}{i + 1}.html', 'wb') as f:
#         f.write(res.content)

# 保存贴吧 对象方式
# class Tieba:
#     def __init__(self):
#         self.url = "https://tieba.baidu.com/f"
#         self.headers = {
#             'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36',
#         }
#
#     def send(self, params):
#         res = requests.get(self.url, headers=self.headers, params=params)
#         return res.text
#
#     def save(self, params, page, con):
#         with open(f'{params['kw']}{page}.html', 'w', encoding='utf-8') as f:
#             f.write(con)
#
#     def run(self):
#         word = input("请输入贴吧名字")
#         pages = int(input("请输入页数"))
#         for page in range(pages):
#             params = {
#                 'kw': word,
#                 'pn': page * 50
#             }
#             data = self.send(params)
#             self.save(params, page, data)
#
#
# te = Tieba()
# te.run()

# post 请求

# cookie 模拟登录

# url = "https://ifanyi.iciba.com/index.php?c=trans&m=fy&client=6&auth_user=key_web_new_fanyi&sign=opMGxMdSV1gaqt2rtPk%2FfNRriqVIAJSQ%2BxmfU0q7dIE%3D"
# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36',
# }
# post_data = {
#     'from': 'zh',
#     'to': 'en',
#     'q': '学习',
# }
# res = requests.post(url, headers=headers, data=post_data)
# # dic = json.loads(res.text)
# print(res.text)

# session 自动处理 cookie
# url = ""
# form = {}
# session = requests.session()
# res = session.post(url, data=form)

# cookie 池

# 代理 ip
proxies = {
    "http": "12.34.56.79:9527",
    # "http": "http://12.34.56.79:9527",
}
url = "https://www.baidu.com"
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36',
}
res = requests.get(url, headers=headers, proxies=proxies)
print(res.content.decode())
